package com.paradise.phone.adapter;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paradise.phone.OnRecyclerViewItemClickListener;
import com.paradise.phone.bean.ContactBean;

import java.util.ArrayList;

/**
 * Created by Sisyphus on 2017/7/14.
 */

public class ContactAdapter extends RecyclerView.Adapter {

    private ArrayList<ContactBean> contacts;
    private char previewShowChar = '\0';
    private OnRecyclerViewItemClickListener listener;

    private RelativeLayout previewClickItem;
    private TextView tvPreviewCall;
    private TextView tvPreviewDelete;

    public ContactAdapter() {
    }

    class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView ivThumPhoto;
        private TextView tvFirstChar;
        private TextView tvName;
        private TextView tvNum;
        private TextView tvCreateTime;
        private TextView tvCall;
        private TextView tvDelete;
        private RelativeLayout llMore;

        private OnRecyclerViewItemClickListener listener;

        public ContactViewHolder(final View itemView, final OnRecyclerViewItemClickListener listener) {
            super(itemView);

            tvFirstChar = (TextView) itemView.findViewById(com.paradise.phone.R.id.tv_first_char);
            ivThumPhoto = (ImageView) itemView.findViewById(com.paradise.phone.R.id.riv_photo);
            tvName = (TextView) itemView.findViewById(com.paradise.phone.R.id.tv_contact_remarks);
            tvCreateTime = (TextView) itemView.findViewById(com.paradise.phone.R.id.tv_create_time);
            tvNum = (TextView) itemView.findViewById(com.paradise.phone.R.id.tv_contact_username);
            tvCall = (TextView) itemView.findViewById(com.paradise.phone.R.id.tv_contact_call);
            tvDelete = (TextView) itemView.findViewById(com.paradise.phone.R.id.tv_contact_delete);
            llMore = (RelativeLayout) itemView.findViewById(com.paradise.phone.R.id.ll_more);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (llMore.getVisibility() == View.VISIBLE) {
                        hideMoreAnim(tvCall, tvDelete, llMore);
                    } else {
                        if (previewClickItem != null
                                && previewClickItem.getVisibility() == View.VISIBLE) {
                            hideMoreAnim(tvPreviewCall, tvPreviewDelete, previewClickItem);
                        }
                        llMore.setVisibility(View.VISIBLE);
                        showMoreAnim(tvCall, tvDelete);
                        previewClickItem = llMore;
                        tvPreviewDelete = tvDelete;
                        tvPreviewCall = tvCall;
                    }
                }
            });

            this.listener = listener;
            tvCall.setOnClickListener(this);
            tvDelete.setOnClickListener(this);
        }

        public ImageView getIvThumPhoto() {
            return ivThumPhoto;
        }

        public TextView getTvFirstChar() {
            return tvFirstChar;
        }

        public TextView getTvName() {
            return tvName;
        }

        public TextView getTvNum() {
            return tvNum;
        }

        public TextView getTvCreateTime() {
            return tvCreateTime;
        }

        @Override
        public void onClick(View v) {
            listener.onRecyclerViewItemClickLitener(v, getAdapterPosition());
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ContactViewHolder(LayoutInflater
                .from(parent.getContext())
                .inflate(com.paradise.phone.R.layout.item_contact, parent, false), listener);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ContactViewHolder cvh = (ContactViewHolder) holder;
        ContactBean cb = contacts.get(position);
        if (cb.getRemarks() != null)
            cvh.getTvName().setText(cb.getRemarks());

        int temp = (int) cb.getPinyin().charAt(0);
        if (cb.getShowFirstChar() != null) {
            cvh.getTvFirstChar().setVisibility(cb.getShowFirstChar() == true ? View.VISIBLE : View.GONE);
            cvh.getTvFirstChar().setText(cb.getFirstChar()+"");
        } else {
            if (temp <= 64
                    || (temp >= 91 && temp <= 96)
                    || (temp >= 123 && temp <= 127)) {
                if (previewShowChar == '\0' || previewShowChar != '#') {
                    cvh.getTvFirstChar().setText("#");
                    cvh.getTvFirstChar().setVisibility(View.VISIBLE);
                    previewShowChar = '#';
                } else {
                    cvh.getTvFirstChar().setVisibility(View.GONE);
                }

            } else if (previewShowChar == '\0' || previewShowChar != cb.getPinyin().charAt(0)) {
                previewShowChar = cb.getPinyin().charAt(0);
                cvh.getTvFirstChar().setText(previewShowChar + "");
                cvh.getTvFirstChar().setVisibility(View.VISIBLE);
            } else {
                cvh.getTvFirstChar().setVisibility(View.GONE);
            }
            cb.setShowFirstChar(cvh.getTvFirstChar().getVisibility() == View.VISIBLE ? true : false);
            cb.setFirstChar(previewShowChar);
        }

        cvh.getTvNum().setText(cb.getNumber());
        cvh.getTvCreateTime().setText(cb.getCreataTime().substring(0, 10) + "创建");
    }

    @Override
    public int getItemCount() {
        return contacts != null ? contacts.size() : 0;
    }

    public void setData(ArrayList<ContactBean> contacts) {
        this.contacts = contacts;
    }

    private void showMoreAnim(TextView tvCall, TextView tvDelete) {
        ObjectAnimator left = ObjectAnimator.ofFloat(tvCall, "translationX", tvCall.getTranslationX(), 250.f);
        ObjectAnimator right = ObjectAnimator.ofFloat(tvDelete, "translationX", tvDelete.getTranslationX(), -250.f);
        ObjectAnimator rotateleft = ObjectAnimator.ofFloat(tvCall, "rotation", 0, 360);
        ObjectAnimator rotateright = ObjectAnimator.ofFloat(tvDelete, "rotation", 0, -360);

        AnimatorSet leftSet = new AnimatorSet();
        leftSet.setDuration(500);
        leftSet.play(left).with(rotateleft);
        AnimatorSet rightSet = new AnimatorSet();
        rightSet.setDuration(500);
        rightSet.play(right).with(rotateright);

        leftSet.start();
        rightSet.start();
    }

    private void hideMoreAnim(TextView tvCall, TextView tvDelete, final RelativeLayout rlMore) {
        ObjectAnimator left = ObjectAnimator.ofFloat(tvCall, "translationX", tvCall.getTranslationX(), -250.f);
        ObjectAnimator right = ObjectAnimator.ofFloat(tvDelete, "translationX", tvDelete.getTranslationX(), 250.f);
        ObjectAnimator rotateleft = ObjectAnimator.ofFloat(tvCall, "rotation", 0, -360);
        ObjectAnimator rotateright = ObjectAnimator.ofFloat(tvDelete, "rotation", 0, 360);

        AnimatorSet leftSet = new AnimatorSet();
        leftSet.setDuration(500);
        leftSet.play(left).with(rotateleft);
        AnimatorSet rightSet = new AnimatorSet();
        rightSet.setDuration(500);
        rightSet.play(right).with(rotateright);

        leftSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                rlMore.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        leftSet.start();
        rightSet.start();
    }

    public void addRecyclerViewItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.listener = listener;
    }

}
